angular.module('myapp.PopupService', [])

.factory('PopupService', function($rootScope, $ionicPopup, $myconfig, $http) {

    return {
        lanya: function(scope) { // 蓝牙连接失败
            var myPopup = $ionicPopup.show({
                templateUrl: 'templates/bitongbao/module/lanya.html',
                scope: scope
            });
            scope.myclose = function() {
                myPopup.close();
            };
            return myPopup;
        },
        tishi: function(scope) { // 提示
            var myPopup = $ionicPopup.show({
                templateUrl: 'templates/bitongbao/module/tishi.html',
                scope: scope
            });
            scope.myclose = function() {
                myPopup.close();
            };
            return myPopup;
        },
        tologin: function(scope) { // 登录提示
            var myPopup = $ionicPopup.show({
                templateUrl: 'templates/bitongbao/module/tologin.html',
                scope: scope
            });
            scope.myclose = function() {
                myPopup.close();
            };
            return myPopup;
        },
        popShare: function(scope) { // 分享提示
            var name_url = window.location.hash;
            var popshare = $ionicPopup.show({
                templateUrl: 'templates/bitongbao/module/share.html',
                scope: scope
            });
            var message_user = JSON.parse(localStorage.getItem('user'));
            var share_type, share_message;
            if (scope.type == '鼻腔' || scope.type == '充能' || scope.type == '经络' || scope.type == '气血' || scope.type == '抗皱美白' || scope.type == '祛斑祛痘' || scope.type == '润肤嫩肤' || scope.type == '面膜导入') {
                share_type = '分享保养成果';
                share_message = '我刚刚做了' + scope.type + '保养，效果很好，你也来试试吧';
            } else {
                share_type = '分享红包';
                share_message = '分享红包';
            }
            //微信分享
            scope.share_wx = function(obj) {

                // $scope.params = '#/dp/' + store_id + '/2' + loadTagIndex;

                // alert(name_url);

                if (obj == 'Timeline') {

                    Wechat.share({
                        message: {
                            title: "健康保养",
                            description: share_message,
                            thumb: "www/img/thumbnail.png",
                            mediaTagName: "TEST-TAG-001",
                            // messageExt: "这是第三方带的测试字段",
                            messageAction: "<action>dotalist</action>",
                            media: {
                                type: Wechat.Type.WEBPAGE,
                                webpageUrl: "http://app.quanminkang.com.cn/manager/share.html?name=" + name_url
                            }
                        },
                        scene: Wechat.Scene.TIMELINE // 分享至朋友圈
                    }, function() {
                        // alert("Success");
                        if (share_type == '分享红包') {
                            $http.post($myconfig.api.base_host + '/sharelogs/share', {
                                "user_id": message_user.id,
                                "shareapp": "微信朋友圈",
                                "sharecontent": share_message + '_red_id=' + scope.redId,
                                "rulename": share_type
                            }).then(function(res) {
                                popshare.close();
                            });
                        } else {
                            $http.post($myconfig.api.base_host + '/sharelogs/share', {
                                "user_id": message_user.id,
                                "shareapp": "微信朋友圈",
                                "sharecontent": share_message,
                                "rulename": share_type
                            }).then(function(res) {
                                popshare.close();
                            });
                        }
                    }, function(reason) {
                        // alert('分享失败');
                    });
                } else if (obj == 'Session') {
                    Wechat.share({
                        message: {
                            title: "健康保养",
                            description: share_message,
                            thumb: "www/img/thumbnail.png",
                            mediaTagName: "TEST-TAG-001",
                            messageExt: "这是第三方带的测试字段",
                            messageAction: "<action>dotalist</action>",
                            media: {
                                type: Wechat.Type.WEBPAGE,
                                webpageUrl: "http://app.quanminkang.com.cn/manager/share.html?name=" + name_url
                            }
                        },
                        scene: Wechat.Scene.SESSION // 分享至好友
                    }, function() {
                        // alert("Success");
                        if (share_type == '分享红包') {
                            $http.post($myconfig.api.base_host + '/sharelogs/share', {
                                "user_id": message_user.id,
                                "shareapp": "微信好友",
                                "sharecontent": share_message + '_red_id=' + scope.redId,
                                "rulename": share_type
                            }).then(function(res) {
                                popshare.close();
                            });
                        } else {
                            $http.post($myconfig.api.base_host + '/sharelogs/share', {
                                "user_id": message_user.id,
                                "shareapp": "微信好友",
                                "sharecontent": share_message,
                                "rulename": share_type
                            }).then(function(res) {
                                popshare.close();
                            });
                        }
                    }, function(reason) {
                        // alert('分享失败');
                    });
                }



            };
            //qq分享
            scope.share_qq = function() {
                var args = {};
                args.scene = QQSDK.Scene.QQ; //QQSDK.Scene.QQZone,QQSDK.Scene.Favorite
                args.url = "http://app.quanminkang.com.cn/manager/share.html?name=" + name_url;
                args.title = "健康保养";
                args.description = share_message;
                args.image = "http://app.quanminkang.com.cn/manager/assets/img/hongbao.svg";
                QQSDK.shareNews(function() {
                    // alert('shareNews success');
                    if (share_type == '分享红包') {
                        $http.post($myconfig.api.base_host + '/sharelogs/share', {
                            "user_id": message_user.id,
                            "shareapp": 'QQ好友',
                            "sharecontent": args.description + '_red_id=' + scope.redId,
                            "rulename": share_type
                        }).then(function(res) {
                            popshare.close();
                        });
                    } else {
                        $http.post($myconfig.api.base_host + '/sharelogs/share', {
                            "user_id": message_user.id,
                            "shareapp": 'QQ好友',
                            "sharecontent": args.description,
                            "rulename": share_type
                        }).then(function(res) {
                            popshare.close();
                        });
                    }
                }, function(failReason) {
                    // alert(failReason);
                    // alert('分享失败');
                }, args);
            };
            scope.sharecloss = function() {
                popshare.close();
            };
            return popshare;
        },
        popSex: function(scope) { // 性别
            var popsex = $ionicPopup.show({
                templateUrl: 'templates/bitongbao/module/sex.html',
                scope: scope
            });
            scope.select = function(_if) {
                if (_if == 0) {
                    scope.sex = {
                        man: {
                            "background": "url('img/icons/man_selected.svg') no-repeat",
                        },
                        woman: {
                            "background": "url('img/icons/woman_default.svg') no-repeat",
                        }
                    }
                    if (scope.userMes) {
                        scope.userMes.sex = _if.toString();
                        $http.post($myconfig.api.base_host + '/users/update_one_user', {
                            "mobile": scope.userMes.mobile,
                            "sex": scope.userMes.sex
                        }).then(function(res) {
                            // console.log(res.data);
                            if (res.data.success) {
                                // console.log('重置');
                                localStorage.clear();
                                localStorage.setItem('user', JSON.stringify(scope.userMes))
                                    // console.log(JSON.stringify(scope.userMes));
                            }
                        });
                    } else {
                        scope.useMes.sex = _if.toString();
                    }
                } else if (_if == 1) {
                    scope.sex = {
                        man: {
                            "background": "url('img/icons/man_default.svg') no-repeat",
                        },
                        woman: {
                            "background": "url('img/icons/woman_selected.svg') no-repeat",
                        }
                    }
                    if (scope.userMes) {
                        scope.userMes.sex = _if.toString();
                        $http.post($myconfig.api.base_host + '/users/update_one_user', {
                            "mobile": scope.userMes.mobile,
                            "sex": scope.userMes.sex
                        }).then(function(res) {
                            // console.log(res.data);
                            if (res.data.success) {
                                // console.log('重置');
                                localStorage.clear();
                                localStorage.setItem('user', JSON.stringify(scope.userMes))
                                    // console.log(JSON.stringify(scope.userMes));
                            }
                        });
                    } else {
                        scope.useMes.sex = _if.toString();
                    }
                }

            };
            scope.sexcloss = function() {
                popsex.close();
            };
            return popsex;
        },
        popSignin: function(scope) {
            var popsignin = $ionicPopup.show({
                templateUrl: 'templates/bitongbao/module/signin.html',
                scope: scope
            });
            scope.signincloss = function() {
                popsignin.close();
            };
            return popsignin;
        },
        CameraS: function(scope) {
            var cameraS = $ionicPopup.show({
                templateUrl: 'templates/bitongbao/module/cameraS.html',
                scope: scope
            });
            scope.selectCam = function(_if) {
                scope.camPlay(_if);
                cameraS.close();
            };
            scope.cameraScloss = function() {
                cameraS.close();
            };
            return cameraS;
        }

    }
});
